/*
 * pic.c
 * 
 * Programmable Interrupt Controller
 * 
 */

#include <pic.h>
#include <i386.h>

void pic_reset()
{
	outb(PIC_1_CTRL_PORT, ICW1_INIT | ICW1_ICW4); 	// IRQs activas por flanco, cascada y ICW4
	outb(PIC_1_DATA_PORT, PIC1_OFFSET); 			// Addr
	outb(PIC_1_DATA_PORT, ICW3_MATTACH2); 			// PIC1 Master, Slave ingresa por IRQ2
	outb(PIC_1_DATA_PORT, ICW4_8086); 				// Modo 8086
	
	outb(PIC_1_DATA_PORT, 0xFF); 					// Enmascara todas las interrupciones

	outb(PIC_2_CTRL_PORT, ICW1_INIT | ICW1_ICW4); 	// IRQs activas por flanco, cascada y ICW4
	outb(PIC_2_DATA_PORT, PIC2_OFFSET); 			// Addr
	outb(PIC_2_DATA_PORT, ICW3_SATTACH2); 			// PIC2 Slave
	outb(PIC_2_DATA_PORT, ICW4_8086); 				// Modo 8086
	
	outb(PIC_2_DATA_PORT, 0xFF); 					// Enmascara todas las interrupciones
}

void pic_enable()
{
	outb(PIC_1_DATA_PORT, 0x00);
	outb(PIC_2_DATA_PORT, 0x00);
}

void pic_disable()
{
	outb(PIC_1_DATA_PORT, 0xFF);
	outb(PIC_2_DATA_PORT, 0xFF);
}

void pic_eoi_1()
{
	outb(PIC_1_CTRL_PORT, PIC_EOI);
}

void pic_eoi_2()
{
	outb(PIC_1_CTRL_PORT, PIC_EOI);
	outb(PIC_2_CTRL_PORT, PIC_EOI);
}
